
/**********************************************************************************\
** 文件名称: MessageOut.c
** 创建人员: YCG
** 创建日期: 2019-02-08
** 文档描述:
** 版本代号: V0.1
** 版本说明: 初始版本
\********************************************************************************/
#include "MessageOut.h"
#include "usbd_cdc_if.h"

#include "fatfs.h"
#include "key.h"
#include "oled.h"
#include "flash.h"
#include "led.h"
#include "heap.h"

extern UART_HandleTypeDef huart1;

StreamBufferHandle_t MessageOutHand;        //定义输出信息的流缓冲区句柄
uint8_t MessageBufOut[1000];
uint8_t MessageBufIn[1000];

void StartMessageOutTask(void const * argument)
{
	
//	  uint8_t*  MessageBufOut =(uint8_t*)MyMalloc(1000);   //申请内存作为缓存空间
    for(;;)
    {
        TimeOut_t xTimeOut;
        TickType_t xTicksToWait = 10;

        vTaskSetTimeOutState( &xTimeOut );

        uint16_t  ySize = xStreamBufferReceive(MessageOutHand, MessageBufOut, 1000, 100);

        do
        {
            if(HAL_UART_Transmit_DMA(&huart1, MessageBufOut, ySize) == HAL_OK) { //启动DMA发送
                break;
            }
        } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
        do
        {
            if(CDC_Transmit_FS(MessageBufOut, ySize) != USBD_BUSY) {
                break;
            }
        } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
    }
}

void Uprintf(char *fmt, ...)
{
    va_list arp;
    va_start (arp, fmt);         /* 将可变长参数转换为va_list */
	 // uint8_t*  MessageBufIn =(uint8_t*)MyMalloc(1000);   //申请内存作为缓存空间
	
    uint16_t  ySize = PrintArray(MessageBufIn, fmt, arp);	   //将数据格式化进缓存中 
    xStreamBufferSend(MessageOutHand, MessageBufIn, ySize, 100);
	
	//  MyFree(MessageBufIn);  //释放内存
    va_end (arp);
}

void Uprintf_Init(void)
{
    MessageOutHand = xStreamBufferCreate(2000, 1);  //初始化缓冲区
}
/********************************End of File************************************/
